package com.lk;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 线程的实现方式：
 * 1.实现Runnable接口
 * 2.继承thread
 * 3.实现callable
 */
public class TestCalable {
    public static void main(String[] args) {
        Test test = new Test();
        FutureTask<Integer> futureTask = new FutureTask(test);
        new Thread(futureTask).start();
        System.out.println("222");
        try {
            Integer result = futureTask.get();
            System.out.println(result);
            System.out.println("---------------------");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        TestRunable testRunable = new TestRunable();
        new Thread(testRunable).start();

        TestThread testThread = new TestThread();
        testThread.start();

    }

    static class Test implements Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            int sum = 0;
            for(int i=0 ; i<1000; i++){
                sum += i;
            }
            return sum;
        }
    }

    static class TestRunable implements Runnable{

        @Override
        public void run() {
            System.out.println("这里是Runnable");
        }
    }

    static class TestThread extends Thread{
        @Override
        public void run() {
            System.out.println("这里是Thread");
        }
    }
}
